package jdk.jdk8.lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 学校想从一批学生中，选出年龄大于等于18，参加过考试并且成绩大于等于80的人去参加比赛。
 */
public class Demo {
    public static void main(String[] args) {
        List<Student> students = initData();
        List<Student> choose = new ArrayList<>();
        //before(students, choose);
        lambda(students, choose);
        choose.stream().forEach(student -> System.out.println("选中：name="+student.getName() +" Age="+student.getAge() +" Score="+student.getScore()));
    }

    private static void lambda(List<Student> students, List<Student> choose) {
        List<Student> list = students.stream()
                .filter(student -> Objects.nonNull(student))
                .filter(student -> Optional.ofNullable(student.getAge()).filter(age -> age >= 18).isPresent())
                .filter(student -> Optional.ofNullable(student.getScore()).filter(score -> score >= 80).isPresent())
                .collect(Collectors.toList());
        choose.addAll(list);
    }

    private static void before(List<Student> students, List<Student> choose) {
        for (Student student : students) {
            if (student.getAge() != null && student.getAge() >= 18) {
                if (student.getScore() != null && student.getScore() >= 80) {
                    choose.add(student);
                }
            }
        }
    }

    public static List<Student> initData(){
        Student s1 = new Student("张三", 19, 80);
        Student s2 = new Student("李四", 19, 50);
        Student s3 = new Student("王五", 23, null);
        Student s4 = new Student("赵六", 16, 90);
        Student s5 = new Student("钱七", null, 99);
        Student s6 = new Student("孙八", 20, 40);
        Student s7 = new Student("吴九", 21, 88);

        return Arrays.asList(s1, s2, s3, s4, s5, s6, s7);
    }


}
